package java51.lesson0619;

import java.io.File;
import java.util.List;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class SearchXml {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		SAXReader reader = new SAXReader();
		try {
			Document doc = reader.read(new File("products.xml"));
			Element root = doc.getRootElement();
			System.out.println("请输入需要查询的商品号：");
			String sch = sc.next();
			List<Element> list = doc.selectNodes("/shop/product");
			for (Element e : list) {
				if ((e.selectSingleNode("itemno").getText()).equals(sch)) {
					String sdis = e.attributeValue("discount");// 是否折扣
					String sfac = e.attributeValue("factoryaddr");// 工厂地址
					// Element eitemno = (Element) e.selectSingleNode("itemno");
					// String itemno = eitemno.getText();// 商品编号
					Element ename = (Element) e.selectSingleNode("name");
					String name = ename.getText();// 商品名称
					Element eprice = (Element) e.selectSingleNode("price");
					String price = eprice.getText();// 商品单价
					Element estockcount = (Element) e.selectSingleNode("stockcount");
					String stockcount = estockcount.getText();// 商品库存
					Element eunit = (Element) e.selectSingleNode("unit");
					String unit = eunit.getText();// 商品计价单位
					System.out.println("折扣:" + sdis + ",工厂地址:" + sfac + ",编号:" + sch + ",名称:" + name + ",单价" + price
							+ ",库存:" + stockcount + ",计价单位：" + unit);
					return;
				}
			}
			System.out.println("不存在该编号商品");
		} catch (DocumentException e) {
			e.printStackTrace();
		}

	}

}
